package com.example.booksystem.biz;

import com.example.booksystem.bean.Book;
import com.example.booksystem.bean.Record;
import com.example.booksystem.bean.Type;
import com.example.booksystem.dao.BookDao;
import com.example.booksystem.dao.RecordDao;
import com.example.booksystem.dao.TypeDao;
import com.example.booksystem.utils.DBUtil;


import java.sql.SQLException;
import java.util.List;

public class BookBiz {
    BookDao bookDao = new BookDao();
    //通过类型查询
    public List<Book> getBooksByTypeId(long id){
        return bookDao.getBooksByTypeId(id);
    }
    //通过页码查询
    public List<Book> getByPage(int page,int size){
        TypeDao typeDao = new TypeDao();
        List<Book> books = bookDao.getByPage(page, size);
        for (Book book : books){
            long typeId = book.getTypeId();
            book.setType(typeDao.getById(typeId));
        }
        return books;
    }
    //通过ID查询
    public Book getById(long id){
        TypeDao typeDao = new TypeDao();
        Book book = bookDao.getById(id);
        Type type = typeDao.getById(book.getTypeId());
        book.setType(type);
        return book;
    }
    //增加
    public int add(long typeId,String name,float price,String desc,String pic,String publish,String author,long stock,String address){
        return bookDao.add(typeId,name,price,desc,pic,publish,author,stock,address);
    }
    //删除
    public int remove(long id) throws Exception {
        int count = 0;
        RecordDao recordDao = new RecordDao();
        List<Record> list = recordDao.getRecordByBookId(id);
        if (list.size()>0){
            throw new Exception("删除的书籍有子信息,删除失败");
        }else {
            count = bookDao.remove(id);
        }
        return count;
    }
    //修改
    public int modify(long typeId,String name,float price,String desc,String pic,String publish,String author,long stock,String address,long id){
        return bookDao.modify(typeId,name,price,desc,pic,publish,author,stock,address,id);
    }

    //书籍类型数量
    public int getCount(){
        return bookDao.getCount();
    }

    public int getPageCount(int pageSize) {
        //1.获取行数
        int rowCount = bookDao.getCount();
        //2.根据行数得到页数,每页多少条
        return  (rowCount-1)/pageSize+1;

    }

    public Book getByName(String name) {
        return bookDao.getByName(name);
    }
}

